package com.michael.leetcode;

import org.junit.Test;

import java.util.*;

public class ZigzagLevelOrder_103 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    /**

    103. 二叉树的锯齿形层序遍历
    给你二叉树的根节点 root ，返回其节点值的 锯齿形层序遍历 。（即先从左往右，再从右往左进行下一层遍历，以此类推，层与层之间交替进行）。
    示例 1：
    输入：root = [3,9,20,null,null,15,7]
    输出：[[3],[20,9],[15,7]]
    示例 2：
    输入：root = [1]
    输出：[[1]]
    示例 3：
    输入：root = []
    输出：[]
    提示：

    树中节点数目在范围 [0, 2000] 内
    -100 <= Node.val <= 100
     */


    // 思路使用双向队列 1 从队列尾部取出当前node 将当前node 的左右子节点从另一端放入队列
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> ans = new LinkedList<List<Integer>>();
        if (root == null) {
            return ans;
        }

        Queue<TreeNode> nodeQueue = new ArrayDeque<TreeNode>();
        nodeQueue.offer(root);
        boolean isOrderLeft = true;

        while (!nodeQueue.isEmpty()) {
            Deque<Integer> levelList = new LinkedList<Integer>();
            int size = nodeQueue.size();
            for (int i = 0; i < size; ++i) {
                TreeNode curNode = nodeQueue.poll();
                if (isOrderLeft) {
                    levelList.offerLast(curNode.val);
                } else {
                    levelList.offerFirst(curNode.val);
                }
                if (curNode.left != null) {
                    nodeQueue.offer(curNode.left);
                }
                if (curNode.right != null) {
                    nodeQueue.offer(curNode.right);
                }
            }
            ans.add(new LinkedList<Integer>(levelList));
            isOrderLeft = !isOrderLeft;
        }
        return ans;
    }

    public List<List<Integer>> zigzagLevelOrder1(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        int d = 0;
        List<Integer> tmp1 = new ArrayList<>();
        tmp1.add(root.val);
        res.add(tmp1);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            if (d ==0){
                List<Integer> tmp = new ArrayList<>();
                if (node.right != null){
                    tmp.add(node.right.val);
                    stack.add(node.right);
                }
                if (node.left != null){
                    tmp.add(node.left.val);
                    stack.add(node.left);
                }
                if (tmp.isEmpty()){
                    continue;
                }
                res.add(tmp);
                d = 1;
            }else {
                List<Integer> tmp = new ArrayList<>();
                if (node.left != null){
                    tmp.add(node.left.val);
                    stack.add(node.left);
                }

                if (node.right != null){
                    tmp.add(node.right.val);
                    stack.add(node.right);
                }
                if (tmp.isEmpty()){
                    continue;
                }
                res.add(tmp);
                d = 0;
            }
        }
        return res;
    }

    private void readLeft(TreeNode node, List<List<Integer>> res, Stack<TreeNode> stack, String d){
        List<Integer> tmp = new ArrayList<>();
        if (node.left != null){
            tmp.add(node.left.val);
            stack.add(node.left);
        }

        if (node.right != null){
            tmp.add(node.right.val);
            stack.add(node.right);
        }
        if (tmp.size() == 0){
            return;
        }
        res.add(tmp);

    }


    private void readRight(TreeNode node, List<List<Integer>> res, Stack<TreeNode> stack, Integer d){
        List<Integer> tmp = new ArrayList<>();
        if (node.right != null){
            tmp.add(node.right.val);
            stack.add(node.right);
        }
        if (node.left != null){
            tmp.add(node.left.val);
            stack.add(node.left);
        }
        if (tmp.size() == 0){
            return;
        }
        res.add(tmp);
    }


    @Test
    public void test(){
        TreeNode f = new TreeNode(15);
        TreeNode s = new TreeNode(7);
        TreeNode n = new TreeNode(9);
        TreeNode t = new TreeNode(20, f,s);
        TreeNode root = new TreeNode(3,n,t);
        List<List<Integer>> lists = zigzagLevelOrder(root);

    }
}